using Gateway.Common.Enums;
using Gateway.Domain.EFCore.Configuration;
using Gateway.Domain.Entities;
using Gateway.Domain.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;

namespace Gateway.Domain.Configuration;

/// <summary>
/// 发起人 配置
/// </summary>
public class WorkFlowDesignSendConfiguration : EntityConfiguration<WorkFlowDesignSend>
{
    public override void Configure(EntityTypeBuilder<WorkFlowDesignSend> builder)
    {
        builder.ToTable(t => t.HasComment("抄送人 配置"));
        builder.Property(x => x.NodeUsers).HasMaxLength(2000).HasComment("抄送人集合")
            .HasConversion(x => JsonConvert.SerializeObject(x),
                p => JsonConvert.DeserializeObject<List<WorkDesignNodeRoleOrUser>>(p));
        builder.Property(x => x.UserSelect).IsFixedLength().HasComment("允许发起人自选抄送人")
            .HasConversion(x => x.Value, p => BooleanStatusEnum.FromValue(p));
        builder.HasOne(x => x.Design).WithOne(p => p.Send)
            .HasForeignKey<WorkFlowDesignSend>(x => x.WorkFlowDesignId)
            .OnDelete(DeleteBehavior.Cascade);
        base.Configure(builder);
    }
}